package segment;

public class NumArray2 {

    private SegmentTree<Integer> segTree;
    public NumArray2(int[] nums) {
        if (nums.length > 0) {
            Integer[] arr = new Integer[nums.length];
            for (int i = 0; i < nums.length; i++)
                arr[i] = nums[i];

            segTree = new SegmentTree<>(arr, (a, b) -> a + b);
        }
    }

    public int sumRange(int i, int j) {
        if(segTree == null)
            throw new IllegalArgumentException("Segment Tree is null");
        return segTree.query(i, j);
    }

    public void update(int i, int val) {
        segTree.set(i, val);
    }

    public static void main(String[] args) {
        int[] nums = {-2,0,3,-5,2,-1};
        NumArray2 na = new NumArray2(nums);
        System.out.println(na.sumRange(2, 5));
    }
}
